package Action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import Beans.Card;
import DAO.CardDao;

@SuppressWarnings("serial")
public class CardAction extends ActionSupport {

	private List<Card> cards;

	CardDao cardDao = new CardDao();

	private int cardID;
	private Card card = new Card();
	private String flag;
	private String value;
	
	/**
	 * 检测参数是否重复
	 * @return
	 * @throws Exception
	 */
	public String checkValue() throws Exception {
		cards = cardDao.checkValue(flag,value);
		if (cards.size() == 0) {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.print("0");
			out.close();
		} else {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.print("1");
			out.close();
		}
		return null;
	}

	/**
	 * 编辑授权卡
	 * 
	 * @return
	 */
	public String editCard() {
		card.setId(cardID);
		card.setStatus(0);
		cardDao.edit(card);
		return "success";
	}

	/**
	 * 删除授权卡
	 * 
	 * @return
	 */
	public String deleteCard() {
		card=cardDao.getCardById(cardID);
		if(card.getStatus()==0){
			cardDao.delete(card);
		}
		return "success";
	}

	/**
	 * 新增授权卡
	 * 待优化，模仿查询手机号页面
	 * @return
	 */
	public String registerCard() {
		card.setSernum(card.getSernum().toUpperCase());
		card.setRfid((card.getRfid().substring(2)).toUpperCase());
		card.setStatus(0);
		cardDao.save(card);
		return "success";
	}

	/**
	 * 通过ID获取授权卡
	 */
	public String getCardById() {
		card = cardDao.getCardById(cardID);
		return "success";
	}

	/**
	 * 获取所有授权卡
	 */
	public String getAllCard() {
		cards = cardDao.getAllCard();
		return "success";
	}

	public List<Card> getCards() {
		return cards;
	}

	public void setCards(List<Card> cards) {
		this.cards = cards;
	}

	public int getCardID() {
		return cardID;
	}

	public void setCardID(int cardID) {
		this.cardID = cardID;
	}

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
}
